package com.star.langchain.learn.tools;

import java.util.List;
import java.util.Map;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

@Service
public class MapTools {


    @Bean
    public McpToolProvider mcpToolProvider() {
        McpClient mapMcpClient = buildMapMcp();
//        McpClient terminalMcp = buildTerminalMcp();

//        最后，您从客户端创建一个 MCP 工具提供者
        McpToolProvider toolProvider = McpToolProvider.builder()
                .mcpClients(mapMcpClient)
                .build();

        // 此外，一个 MCP 服务器可能经常提供数十个工具，而给定的 AI 服务可能只需要其中几个，既是为了防止使用不希望的工具，也是为了减少幻觉的可能性。《McpToolProvider》允许通过名称过滤这些工具，如下所示：
//        McpToolProvider toolProvider = McpToolProvider.builder()
//                .mcpClients(mcpClient)
//                .filterToolNames("get_issue", "get_issue_comments", "list_issues")
//                .build();
        return toolProvider;
    }


    private McpClient buildMapMcp() {
        //        对于 stdio - 此示例展示了如何以子进程的方式从 NPM 包中启动一个服务器
        McpTransport transport = new StdioMcpTransport.Builder()
                .command(List.of("npx", "-y", "@amap/amap-maps-mcp-server"))
                .environment(
                        Map.of("AMAP_MAPS_API_KEY", "e33a13cbffb220240ecd276f78fb72eb"))
                .logEvents(true) // only if you want to see the traffic in the log
                .build();

//        要从传输创建 MCP 客户端
        McpClient mcpClient = new DefaultMcpClient.Builder()
                .key("MyMCPClient")
                .transport(transport)
                .build();
        return mcpClient;
    }

    ;

    private McpClient buildTerminalMcp() {
//        对于 stdio - 此示例展示了如何以子进程的方式从 NPM 包中启动一个服务器

//        "terminal-mcp-server": {
//            "command": "npx",
//                    "args": [
//            "-y",
//                    "@smithery/cli@latest",
//                    "run",
//                    "@weidwonder/terminal-mcp-server",
//                    "--key",
//                    "5cfc9b6b-24a3-45d1-a491-d9f12b5b7efe",
//                    "--profile",
//                    "advanced-mule-zgkMLe"
//      ]
//        }
//    }
        McpTransport transport = new StdioMcpTransport.Builder()
//                .command(List.of("node", "/Users/star/IdeaProjects/terminal-mcp-server/build/index.js"))
                .command(List.of("npx", "-y", "@smithery/cli@latest", "run",
                        "@weidwonder/terminal-mcp-server", "--key", "5cfc9b6b-24a3-45d1-a491-d9f12b5b7efe",
                        "--profile", "advanced-mule-zgkMLe"))
                .logEvents(true) // only if you want to see the traffic in the log
                .build();

//        要从传输创建 MCP 客户端
        McpClient mcpClient = new DefaultMcpClient.Builder()
                .key("TerminalMCPClient")
                .transport(transport)
                .build();

        return mcpClient;

    }


}
